﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace DAL.Help
{
    public class ConvertType
    {
        public static int ToInt32(object value)
        {
            try
            {
                return Convert.ToInt32(value);
            }
            catch
            {
                return 0;
            }
        }

        public static string ToString(object value)
        {
            try
            {
                return Convert.ToString(value);
            }
            catch
            {
                return "";
            }
        }

        public static DateTime ToDateTime(object value)
        {
            try
            {
                return Convert.ToDateTime(value);
            }
            catch
            {
                return DateTime.Now;
            }
        }

        // chuyen chuoi ngay thang theo dinh dang tuy y
        // dd/MM/yyyy hh:mm:ss tt
        // dd/MM/yyyy HH:mm:ss

        public static DateTime ToDateTime(object value, string format)
        {
            try
            {
                DateTimeFormatInfo di = new DateTimeFormatInfo();
                di.ShortDatePattern = format;
                return Convert.ToDateTime(value, di);
            }
            catch
            {
                return DateTime.Now;
            }
        }

        public static DateTime? ToNullableDateTime(object value)
        {
            try
            {
                if (value == null)
                    return null;
                return Convert.ToDateTime(value);
            }
            catch
            {
                return null;
            }
        }

    }
}
